package com.bjpowernode;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.bjpowernode.common.client.DfsClient;
import com.bjpowernode.common.client.RedisClient;
import com.bjpowernode.common.constant.RedisKey;
import com.bjpowernode.db.mapper.BidInfoMapper;
import com.bjpowernode.mvc.config.RealnameConfig;
import com.bjpowernode.mvc.config.SmsConfig;
import com.bjpowernode.db.result.RankResult;
import jakarta.annotation.Resource;
import org.csource.common.MyException;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.io.IOException;
import java.util.List;

@EnableScheduling
@EnableConfigurationProperties({
        SmsConfig.class, RealnameConfig.class
})
@MapperScan(basePackages = "com.bjpowernode.db.mapper")
@SpringBootApplication
public class YinglibaoApplication implements CommandLineRunner {

    @Value("${dfs.http-url}")
    private String DFS_NGINX_HTTP_URL;

    //容器的初始化，获取容器的Bean
    @Resource
    private ApplicationContext applicationContext;

    //注入redis操作类
    @Resource
    private RedisClient redisClient;


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        //设置数据库的类型为Mysql
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    public static void main(String[] args) {
        SpringApplication.run(YinglibaoApplication.class, args);
    }

    /**
     * 在spring容器初始化完成后自动执行，可以获取到容器中的Bean对象
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {

        //在项目启动时执行，读取数据库中的数据，初始化Redis排行榜
        BidInfoMapper bidInfoMapper = applicationContext.getBean(BidInfoMapper.class);

        //查询投资的用户手机号和投资金额
        List<RankResult> rankResultList = bidInfoMapper.selectMoneyRank();
        rankResultList.forEach(rank -> {
            redisClient.addZSet(RedisKey.BID_RANK_LIST, rank.getPhone(), rank.getMoney());
        });
    }

    /**
     * 通过Bean的方式初始化生成DfsClient客户端
     *
     * @return
     * @throws MyException
     * @throws IOException
     */
    @Bean
    public DfsClient dfsClient() throws MyException, IOException {
        DfsClient.HTTP_URL = DFS_NGINX_HTTP_URL;
        DfsClient dfsClient = new DfsClient();
        dfsClient.initDfs();
        return dfsClient;
    }

}
